const axios = require("axios")

module.exports = {
    is_not_in_arr(_arr, _key, _v) {
        let flag = false; 
        _arr.map(item=>{
            if(_v == item[_key]) {
                flag = true
            }
        })
        return flag
    },
    getData(obj) {
        return new Promise(async (resolve, reject) => {
            // https://space.bilibili.com/482937140/video?tid=0&page=1&keyword=&order=pubdate
            // https://api.bilibili.com/x/space/arc/search?mid=258457966&ps=30&tid=0&pn=2&keyword=&order=pubdate&jsonp=jsonp
            // mid=up主的id ps=是每页多少条视频=将其改成x确保尽可能一次拿到所有的昨天投的视频
            try {
                axios.get(
                    // url: `https://api.bilibili.com/x/space/arc/search?mid=${obj.id}&ps=100&tid=0&pn=1&keyword=&order=pubdate&jsonp=jsonp`,
                    `https://api.bilibili.com/x/space/arc/search?mid=1468726245&ps=100&tid=0&pn=1&keyword=&order=pubdate&jsonp=jsonp`,
                ).then(
                    (res) => {
                        let resList = res.data.data.list.vlist
                        console.log(" ~ 拿到的所有数据 resList.length", resList.length)
                        let result = []

                        // 找近 x 条中是昨天投稿的
                        for (let z = 0; z < resList.length; z++) {
                            const iitteemm = resList[z];
                            result.push(iitteemm)
                        }
                        console.log(" ~ result.length", result.length)
                        // 根据播放量 排序 从大到小
                        result.sort((a, b) => {
                            return b.play - a.play
                        })
                        // 拿前十
                        if (result.length > 10) {
                            result.length = 10
                        }
                        resolve(result)
                    }
                )
            } catch (error) {
                console.log("xxxx 获取前十播放量的视频出错 xxxx")
                resolve([])
            }
        })

    }
}